<template>
	<u-popup :show="visible" round="16rpx" mode="bottom" @close="hide">
		<view class="product-category-popup">
			<view class="product-category-popup__head">
				<text>产品类别</text>
				<view class="product-category-popup__head-close bg-hover" @tap="hide">
					<u-image width="32rpx" height="32rpx"
						src="https://file.casugn.com/storecompcard/static/img/tips／close@2x_1681978220794.png"></u-image>
				</view>
			</view>
			<view class="product-category-popup__form">
				<u-form ref="uForm" :model="model" :rules="rules" labelWidth="180" labelAlign="left" labelPosition="left">
					<u-form-item label="产品类别" prop="category" borderBottom required>
						<u-input v-model="model.category" border="none" placeholder="不超过10个字" maxlength="10"
							:customStyle="inputCustomStyle"></u-input>
					</u-form-item>
					<u-form-item label="排序" prop="sort" labelWidth="100" borderBottom>
						<u-input v-model="model.sort" border="none" placeholder="请输入序号" type="number"
							:customStyle="inputCustomStyle"></u-input>
					</u-form-item>
				</u-form>
			</view>

			<view class="submit-box">
				<u-button v-if="model.id" color="#276AEE" text="保存" throttleTime="500" @click="submit"></u-button>
				<u-button v-else color="#276AEE" text="提交" throttleTime="500" @click="submit"></u-button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import { mapGetters } from 'vuex'
	export default {
		options: {
			styleIsolation: 'shared', // 解除样式隔离
		},
		data() {
			return {
				inputCustomStyle: { padding: '10rpx 0' },
				visible: false,
				rules: {
					category: [{
						required: true,
						max: 10,
						message: '请输入正确的产品类别',
						trigger: ['blur', 'change'],
					}, ],
				},
				model: {
					category: '',
					sort: '',
				},

				loading: false,
			}
		},
		computed: {
			...mapGetters(['userInfo']),
		},
		methods: {
			setRules() {
				this.$refs.uForm.setRules(this.rules)
			},
			add() {
				this.edit({})
			},
			edit(record) {
				this.visible = true
				this.model = JSON.parse(JSON.stringify(record))

				this.$nextTick(() => {
					this.$refs.uForm.setRules(this.rules)
				})
			},
			hide() {
				this.visible = false
			},
			submit() {
				const that = this
				this.loading = true
				this.$refs.uForm.validate().then(async success => {
					if (!that.userInfo.companyId) {
						uni.showToast({ title: '无对应公司信息!请联系公司管理员', icon: 'none' })
						that.loading = false
						return
					}
					let params = {
						...that.model,
						companyId: that.userInfo.companyId || '',
						openId: that.userInfo.openId,
						isDel: 0,
					}

					let res = {}
					if (params.id) {
						res = await this.$api.editProductCategory(params)
					} else {
						res = await that.$api.addProductCategory(params)
					}
					that.loading = false
					if (res.success) {
						uni.showToast({ title: '提交成功！', icon: 'none' });
						that.hide()
						that.$emit('refresh')
					} else {
						uni.showToast({ title: res.message || '提交失败！', icon: 'none' });
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
					that.loading = false
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.product-category-popup {

		.product-category-popup__head {
			position: relative;
			height: 92rpx;
			font-size: 32rpx;
			font-weight: 600;
			color: #1D2129;
			line-height: 44rpx;

			display: flex;
			justify-content: center;
			align-items: center;

			.product-category-popup__head-close {
				position: absolute;
				top: 30rpx;
				right: 32rpx;
			}
		}

		::v-deep.product-category-popup__form {
			padding: 24rpx 32rpx;

			.u-form-item__body__left__content__required {
				position: relative !important;
				left: 0 !important;
				padding-right: 10rpx !important;
			}
		}

		.submit-box {
			padding: 0 32rpx 40rpx;
		}
	}
</style>
